const { default: api } = require("../../utils/api");
const message = require("../../utils/message");

// pages/goods/index2.js
Page({

    /**
     * 页面的初始数据
     */
    data: {

        category_list:[],
        tab_index:-1,
        category_id:0,
        page_index:1,
        product_list:[],
        has_load:false,


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        this.getCategoryList();

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getCategoryProductList:function(  ){

        message.show_loading();
        api.product.getCategoryProductList( (result)=>{
          message.hide_loading();
          this.setData({
            category_product_list:result.data
          })
    
          this.data.category_product_list.map( (item , index)=>{
            wx.createSelectorQuery().select("#item_"+item.id).boundingClientRect( function(rect){
              item.offsetTop = rect.top;
              item.height = rect.height;
            } ).exec();
    
          } )
    
        })
    
      },

      getCategoryList:function(){
          var me = this;
          var params = {};
          params.pid=0;
          api.product.getCategoryList( params , function( response ){
            if(response.status==1){
                me.setData({
                    category_list: response.data.category_list,
                    product_list: response.data.product_list
                })
            }else{
                message.show_error( response.message );
            }
          })
    },
    selectTab:function( e ){
        var index = e.currentTarget.dataset.index;
        var id = e.currentTarget.dataset.id;
        this.setData({
            tab_index: index,
            category_id: id, 
            has_load:false,
            no_data:false,
            product_list:[]
        })

        this.getProductListByCategory();
    },
    getProductListByCategory: function (  ) {
        var me = this;

        var params = {}
        params.category_id = this.data.category_id;

        var page_index = this.data.page_index;
        if(this.data.has_load){

            page_index++;
        }
        params.page_index = page_index;
        message.show_loading();
        api.product.getProductListByCategory( params , function( response ){
            message.hide_loading();

            if( response.status==1 ){


                var no_data = false;

                if(response.data.length<10){
                    no_data = true
                }
                var product_list = me.data.product_list;
                product_list = product_list.concat( response.data );

                me.setData({
                        product_list:product_list,
                        has_load:true,
                        page_index:page_index,
                        no_data: no_data,
                        
                });

            }else{



            }
        })

    },
    getMore:function(){
        //加载更多
        this.getProductListByCategory();
    }
})